package com.likou;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: 陈桂明
 * Date: 2025/8/31 12:48
 * Description: q73
 */
public class q73 {
    public static void main(String[] args) {
        q73 q73 = new q73();
        int[][] matrix = new int[][]{new int[]{1, 1, 1}, new int[]{1, 0, 1}, new int[]{1, 1, 1}};
        q73.setZeroes(matrix);
    }

    public void setZeroes(int[][] matrix) {
        if (matrix.length == 0) {
            return;
        }

        int m = matrix.length;
        int n = matrix[0].length;
        int[] zero = new int[n];
        for (int i = 0; i < n; i++) {
            zero[i] = 0;
        }
        // 记录 0 的坐标
        Map<Integer, List<Integer>> map = new HashMap<>();
        int h = -1;
        for (int i = 0; i < m * n; i++) {
            int lie = i % n;
            if (lie == 0) {
                h++;
            }
            if (matrix[h][lie] == 0) {
                if (map.containsKey(h)) {
                    map.get(h).add(lie);
                } else {
                    List<Integer> list = new ArrayList<>();
                    list.add(lie);
                    map.put(h, map.getOrDefault(h, list));
                }
            }
        }

        // 变化为 0
        for (Integer key : map.keySet()) {
            matrix[key] = zero;
            List<Integer> values = map.get(key);
            for (Integer lie : values) {
                for (int k = 0; k < m; k++) {
                    matrix[k][lie] = 0;
                    k++;
                }
            }
        }
    }
}
